import 'package:flutter/material.dart';
import 'package:flutter_demo1/tools/keepAliveWrapper.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

class _HomePageState extends State<HomePage>
    with SingleTickerProviderStateMixin {
  late TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = TabController(length: 5, vsync: this);
    _tabController.addListener(() {
      if (_tabController.animation!.value == _tabController.index) {
        print("选择标签:${_tabController.index}");
      }
      print("选择标签======>:${_tabController.index}");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        title: TabBar(
            indicatorColor: Colors.red,
            labelColor: Colors.red,
            controller: _tabController,
            tabs: const [
              Tab(
                child: Text("关注"),
              ),
              Tab(
                child: Text("推荐"),
              ),
              Tab(
                child: Text("热搜"),
              ),
              Tab(
                child: Text("发现"),
              ),
              Tab(
                child: Text("视频"),
              ),
            ]),
      ),
      body: TabBarView(
        controller: _tabController,
        children: [
          KeepAliveWrapper(
            child: ListView(
              children: const [
                ListTile(
                  title: Text("第1tab"),
                ),
                ListTile(
                  title: Text("第2tab"),
                ),
                ListTile(
                  title: Text("第3tab"),
                ),
                ListTile(
                  title: Text("第4tab"),
                ),
                ListTile(
                  title: Text("第5tab"),
                ),
                ListTile(
                  title: Text("第6tab"),
                ),
                ListTile(
                  title: Text("第7tab"),
                ),
                ListTile(
                  title: Text("第8tab"),
                ),
                ListTile(
                  title: Text("第9tab"),
                ),
                ListTile(
                  title: Text("第10tab"),
                ),
                ListTile(
                  title: Text("第11tab"),
                ),
                ListTile(
                  title: Text("第12tab"),
                ),
                ListTile(
                  title: Text("第14tab"),
                ),
                ListTile(
                  title: Text("第15tab"),
                ),
              ],
            ),
          ),
          const KeepAliveWrapper(
              child: Center(
            child: Text("推荐"),
          )),
          const KeepAliveWrapper(
              child: Center(
            child: Text("热搜"),
          )),
          const KeepAliveWrapper(
              child: Center(
            child: Text("发现"),
          )),
          const KeepAliveWrapper(
              child: Center(
            child: Text("视频"),
          )),
        ],
      ),
    );
  }
}
